package com.cac.demo.controller.msConsole;/*
 *@program:test-curd
 *@description:分拣台基础信息接口
 *@author: lsl
 *@Time: 2022/10/20  17:20
 */


import com.alibaba.fastjson.JSON;
import com.cac.demo.controller.dispatchSearch.MsDeliveryPackageInfoRest;
import com.cac.demo.dao.msConsole.MsConsoleDao;
import com.cac.demo.service.msConsole.MsConsoleService;


import com.cac.demo.util.adapter.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/MsConsoleRest")
@RestController
@Component
@Slf4j
public class MsConsoleRest {
    @Autowired
    private MsConsoleService msConsoleService;
    @Autowired
    private Environment environment;
    @Autowired
    private MsDeliveryPackageInfoRest msDeliveryPackageInfoRest;
    @Autowired
    private MsConsoleDao msConsoleDao;
    @PostMapping("/searchSortingTableByPage")
    public ResponseMsg<QueryRespBean<HashMap>> searchSortingTableByPage(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        QueryRespBean<HashMap> result = msConsoleService.searchSortingTableByPage(queryReqBean);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    @PostMapping("/searchReceiveTableByPage")
    public ResponseMsg<QueryRespBean<HashMap>> searchReceiveTableByPage(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        QueryRespBean<HashMap> result = msConsoleService.selectReceiveTableByPage(queryReqBean);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 分配工作包任务到分拣台
     *
     * @return
     */
    @PostMapping("/distributionTask")
    public ResponseMsg<String> distributionTask() {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setRetCode(msConsoleService.distributionTask());
        responseMsg.setMessage("操作成功！");
        return responseMsg;
    }

    //调用分配分拣任务存储过程
    @PostMapping("/callBle")
    @Scheduled(cron = "0 30 2 * * ?")
    public ResponseMsg<String> callBle() {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        long starttime = System.currentTimeMillis();
        String taskId = ComUtil.getId();
        msConsoleDao.addTask(taskId,"ALL");
        HashMap result = msConsoleService.callBle();
        log.info("存储过程返回值："+result.toString());
        if (result.get("return_two").equals("执行成功")) {
            responseMsg.setRetCode("200").setMessage("执行分配分拣任务成功！");
            msDeliveryPackageInfoRest.upDeliveryStatus();
        } else {
            responseMsg.setMessage("执行分配分拣任务失败！").setRetCode("201");
        }
        long endtime = System.currentTimeMillis();
        HashMap ret = new HashMap();
        ret.put("taskId",taskId);
        ret.put("retCode",responseMsg.getRetCode());
        ret.put("retMsg",responseMsg.getMessage());
        ret.put("costTime",((endtime-starttime)/1000)+"s");
        msConsoleDao.updateTask(ret);
        return responseMsg;
    }

    @GetMapping("/callBle")
    //页面调用
    public ResponseMsg<String> callBleByGet() {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        long starttime = System.currentTimeMillis();
        String taskId = ComUtil.getId();
        msConsoleDao.addTask(taskId,"ALL");
        HashMap result = msConsoleService.callBle();

        if (result.get("return_two").equals("执行成功")) {
            responseMsg.setRetCode("200").setMessage("执行分配分拣任务成功！");
            msDeliveryPackageInfoRest.upDeliveryStatus();
        } else {
            responseMsg.setMessage("执行分配分拣任务失败！").setRetCode("201");
        }
        long endtime = System.currentTimeMillis();
        HashMap ret = new HashMap();
        ret.put("taskId",taskId);
        ret.put("retCode",responseMsg.getRetCode());
        ret.put("retMsg",responseMsg.getMessage());
        ret.put("costTime",((endtime-starttime)/1000)+"s");
        msConsoleDao.updateTask(ret);
        return responseMsg;
    }

    @GetMapping("testAlgori")
    public String testAlgori() {
        msConsoleService.algorithmTask();
        return "调用完毕";
    }

    /**
     * 更新操作台工作状态 上线
     *
     * @return
     */
    @PostMapping("/updateOnlineStatus")
    public ResponseMsg<String> updateOnlineStatus(@RequestBody List<HashMap> list) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String result = "201";
        try {
            msConsoleService.updateOnlineStatus(list);
            result = "200";
        } catch (Exception e) {
            e.printStackTrace();
            result = "201";
        }
        if (result.equals("200")) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("操作成功！");
        } else {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc("操作失败，该电脑未绑定为工作台！");
        }
        return responseMsg;
    }

    /**
     * 更新分拣台工作状态 下线
     *
     * @return
     */
    @PostMapping("/updateOfflineStatus")
    @Transactional
    public ResponseMsg<String> updateOfflineStatus(@RequestBody List<HashMap> list) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        msConsoleService.updateOfflineStatus(list);
        return responseMsg;
    }

    /**
     * 新增操作台
     *
     * @param map
     * @return
     */
    @PostMapping("/insertNewConsole")
    public ResponseMsg<String> insertNewConsole(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        map.put("IP", IP);
        Integer sqlResult = msConsoleService.insertNewConsole(map);
        if (sqlResult == 1) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("操作成功！");
        } else {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("操作失败！");
        }
        return responseMsg;
    }

    /**
     * 更新操作台信息
     *
     * @param map
     * @return
     */
    @PostMapping("/updateMsConsole")
    public ResponseMsg<String> updateMsConsole(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        Integer sqlResult = msConsoleService.updateMsConsole(map);
        if (sqlResult == 1) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("操作成功！");
        } else {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("操作失败！");
        }
        return responseMsg;
    }

    /**
     * 删除操作台信息
     *
     * @param
     * @return
     */
    @PostMapping("/deleteMsConsole")
    public ResponseMsg<String> deleteMsConsole(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        List<String> ids = (List<String>) map.get("data");
        Integer sqlResult = msConsoleService.deleteConsole(ids);
        if (sqlResult == 1) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("删除成功！");
        } else {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("删除失败！");
        }
        return responseMsg;
    }

    /**
     * 手动指派任务
     *
     * @param map
     * @return
     */
    @PostMapping("/changeTaskToMsConsole")
    public ResponseMsg<String> changeTaskToMsConsole(@RequestBody HashMap map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String sqlResult = msConsoleService.changeTaskToMsConsole(map);
        if (sqlResult != null && sqlResult.equals("200")) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("操作成功！");
        } else {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("操作失败！");
        }
        return responseMsg;
    }

    @PostMapping("/selectMsConsloeName")
    public ResponseMsg<List<String>> selectMsConsloeName() {
        ResponseMsg<List<String>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msConsoleService.selectMsConsloeName());
        return responseMsg;
    }
}
